import Vue from "vue";
import Vuex from "vuex";
import router from "../router";
import {
	api
} from "@/store/axiosConfig.js"; //导入 axios 配置
Vue.use(Vuex);
let baseInfo = JSON.parse(localStorage.getItem("baseInfo")) || {};
let check_id = localStorage.getItem('check_id')

let is_login = false;
let show_login = false;
if (localStorage.getItem('token') && localStorage.getItem('user_id')) {
	is_login = true;
} else {
	is_login = false;
}
show_login = !is_login;
if (SHOPID) localStorage.setItem('shop_id', SHOPID);
let shop_id = localStorage.getItem('shop_id');

export default new Vuex.Store({
	state: {
		baseInfo,
		shop_id,
		check_id,
		is_login,
		webConfig: {},
		isCollapse: false,
		show_login,
	},
	getters: {},
	mutations: {
		set_vuex_data(state, opt) {
			let {
				key,
				val
			} = opt;
			state[key] = val;
		},
		change_store_keyword(state, val) {
			state.store_keyword = {
				time: new Date().getTime(),
				keyword: val,
			};
		},
		//底部导航
		set_webConfig(state, value) {
			// 设置网站名称
			// if (value.site_name) document.title = value.site_name;
			// if (value.site_seo_descrition) {
			// 	const metaDescription = document.querySelector('meta[name="description"]');
			// 	metaDescription.setAttribute('content', value.site_seo_descrition);
			// }
			// if (value.site_seo_keywords) {
			// 	const metaKeywords = document.querySelector('meta[name="keywords"]');
			// 	metaKeywords.setAttribute('content', value.site_seo_keywords)
			// }
			state.webConfig = value;
		},

		//清空登陆信息
		logout(state) {
			state.is_login = false;
			state.baseInfo = {};


			localStorage.removeItem('check_id');
			localStorage.removeItem('shop_id');
			localStorage.removeItem('uuid');
			localStorage.removeItem('token');
			localStorage.removeItem('user_id');
		},

		//改变meta
		CHANGE_META_INFO(state, metaInfo) {
			state.metaInfo = metaInfo;
		},

		//设置基本信息
		set_baseInfo(state, data) {
			state.is_login = true;
			state.baseInfo = data;
			localStorage.setItem("baseInfo", JSON.stringify(data));
		},
		set_logoAuth(state, value) {
			state.logoAuth = value;

		},
		set_is_login(state, value) {
			state.is_login = value;
		},
	},

	actions: {
		async appInit({
			commit,
			state,
			dispatch
		}, data) {
			dispatch("appInitGetAssets");
			let token = localStorage.getItem("token");
			let user_id = localStorage.getItem("user_id");
			if (token) {
				commit("set_is_login", true);
				dispatch("getUserloginedInfo");
			} else {
				commit("set_is_login", false);
			}
		},

		//获取登陆后的信息
		async getUserloginedInfo({
			commit,
			state,
			dispatch
		}, data) {
			dispatch("getUserInfo");
		},




		//初始化资源
		async appInitGetAssets({
			commit,
			state,
			dispatch
		}, data) {
			dispatch('getDictData');
			api('getProject').then((res) => {
				if (res.code == 1) {
					state.mix_proData = res.data;

				}
			});
			//网站配置信息
			api("setting").then((res) => {
				if (res.code === 1) {
					commit("set_webConfig", res.data);
				}
			});

		},

		async getDictData({
			commit,
			state,
			dispatch
		}, data) {
			return new Promise((resolve, reject) => {
				api('dictData').then((res) => {
					if (res.code == 1) {
						state.mix_dictData = res.data;
						resolve(res.data);
					} else {
						reject(res);
					}
				});
			});
		},

		// 获取用户信息
		async getUserInfo({
			commit,
			state,
			dispatch
		}) {
			return new Promise((resolve, reject) => {
				api("getUserInfo", {}, 'get').then((res) => {
					const {
						code,
						data,
						msg
					} = res;
					if (code === 1) {
						resolve(data);
						commit("set_is_login", true);
						commit("set_baseInfo", data);
						dispatch("getApply");
					} else {
						commit("set_is_login", false);
					}
				});
			});

		},
	},
});